package com.hu.green.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hu.green.commen.R;
import com.hu.green.entity.Garden;
import com.hu.green.service.GardenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/garden")
@CrossOrigin(value = "*")
public class GardenController {

    @Autowired
    private GardenService gardenService;

    /**
     * 添加花园
     * @param garden
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody Garden garden){
        // 1. 建立数据
        gardenService.save(garden);
        // 2. 返回数据
        return R.success("添加成功");
    }
    /**
     * 获取
     * @param account
     * @return
     */
    @PostMapping("/{account}")
    public List<Garden> getGardenMessage(@PathVariable String account){
        return gardenService.get(account);
    }

    /**
     * 删除
     * @param id
     * @return
     */

    @DeleteMapping("/{id}")
    public List<Garden> deleteGardenMessage(@PathVariable int id){
        LambdaQueryWrapper<Garden> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Garden::getId,id);
        String account = gardenService.getOne(queryWrapper).getAccount();
        gardenService.removeById(id);
        return gardenService.get(account);
    }
}
